<?

class FieldConstructor {

    public static function GetFieldHtml($field) {
        if($field['restricted'] == Y && !$GLOBALS['USER']->isAdmin()){
            return;
        }
        $str = "";
        $type = FLDTypes::Get($field['type_id']);
        if ($type['code'] == "text") {
            $required = $field['required'] ? "<span class=\"required\">*</span>" : false;
            $label = "<label for=\"value[".$field['value_id']."]\">".$field['title']."$required: </label>";
            $value = !empty($field['value']) ? $field['value'] : (isset($field['default_value']) ? $field['default_value'] : false) ;
            $str = $label."<input type=\"text\" name=\"field[" . $field['id'] . "][".$field['value_id']."]\" value=\"$value\" />";
        }elseif ($type['code'] == "file") {
            $required = $field['required'] ? "<span class=\"required\">*</span>" : false;
            $label = "<label for=\"value[".$field['value_id']."]\">".$field['title']."$required: </label>";
            $value = !empty($field['value']) ? $field['value'] : (isset($field['default_value']) ? $field['default_value'] : false) ;
            if($value){
                $file = Filesystem::GetFile($value);
                $str = "<div class=\"preview-img\"><img src=\"".  $file['uri'] . "\" /></div>";
            }
            $str .= $label."<input type=\"file\" name=\"field[" . $field['id'] . "][".$field['value_id']."]\"/>";            
        }
        return "<div class=\"input\">".$str."</div>";
    }
}
